package com.gameGroups.Controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.reflection.wrapper.CollectionWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gameGroups.Dto.UserInfoDto;
import com.gameGroups.Service.UserLoginService;
import com.gameGroups.Utils.ImageUtil;
import com.gameGroups.Utils.MyException;
import com.gameGroups.Utils.StringUtil;

@Controller
@RequestMapping("/user")
public class UserLoginController {
  @Autowired
  private UserLoginService userLoginService;
  //登录
  @RequestMapping(value="/loginUser.do",method=RequestMethod.POST)
  @ResponseBody
  public Map<String,Object> userLogin(HttpServletRequest request,HttpServletResponse response) throws MyException{
	 Map<String,Object> model=new HashMap<String, Object>(); 
	 try {
		 String userName=request.getParameter("userName");
		 String tel=request.getParameter("tel");
		 String passWord=request.getParameter("passWord");
		 String loginName=userName==null?tel:userName;
		 model.put("loginName", loginName);
		 model.put("passWord", passWord);
		 UserInfoDto userInfo=userLoginService.queryUserLogin(model);
		 if (StringUtil.isNullOrBlank(userInfo)) {
		  model.put("status", "5555");
		  model.put("errorMessage", "账号或密码错误！");
		 }else{
		  model.put("status", "0000"); 
		  model.put("userInfo", userInfo);
		  model.put("errorMessage", "欢迎大佬！");
		 }		
	} catch (Exception e) {
		model.put("status", "9999");
		model.put("errorMessage", "系统异常!");
		throw new MyException("登录功能异常!"+":"+e);
	}

	 return model;  
  }
  //注册
  @RequestMapping(value="/saveUser.do",method=RequestMethod.POST)
  @ResponseBody
  public Map<String,Object> userSave(HttpServletRequest request,HttpServletResponse response) throws MyException{
	  Map<String,Object> model=new HashMap<String, Object>();
	  try {
			 String userName=request.getParameter("userName");
			 String passWord=request.getParameter("passWord");
			 String city=request.getParameter("city");
//			 String code=(String) request.getSession().getAttribute("code");
//		     if (!code.equalsIgnoreCase(request.getParameter("code"))) {
//			   model.put("status", "3333");
//			   request.getSession().removeAttribute("code");
//			   return model;
//			 }
			 Integer userNum=userLoginService.queryUserNum();
			 String pName="会员"+userNum;
			 model.put("userName", userName);
			 model.put("passWord", passWord);
			 model.put("city", city);
			 model.put("pName", pName);
             userLoginService.saveUser(model);
             model.put("status", "0000");		  	
		} catch (Exception e) {
			model.put("status", "9999");
			model.put("errorMessage", "系统异常!");
			throw new MyException("注册功能异常!"+":"+e);
		}
		 return model;
  }
  
  //登录名唯一性异步请求验证
  @RequestMapping(value="/userExist.do",method=RequestMethod.POST)
  @ResponseBody
  public  Map<String,Object> queryUserExist(HttpServletRequest request,HttpServletResponse response) throws MyException{
	  Map<String,Object> model=new HashMap<String, Object>();
	  try {
		  String userName=request.getParameter("userName");
		  Integer userExist=userLoginService.queryUserExist(userName);
		  if (userExist>0) {
			     model.put("status", "5555"); 
				 model.put("errorMessage", "用户名已存在!");				  
			  }else{
				 model.put("status", "0000");	
			 }
		} catch (Exception e) {
		model.put("status", "9999");
		model.put("errorMessage", "系统异常!");
		throw new MyException("登录名唯一性验证功能异常!"+":"+e);
	    }
	  return model;
  }
  
  //调用验证码生成并把验证码返回页面  
  @RequestMapping(value="/createCode.do",method=RequestMethod.POST)
  public void createCode(HttpServletRequest request,HttpServletResponse response) throws IOException{
	  OutputStream ops = null; 
      try {
          Map<String, BufferedImage> imageMap = ImageUtil.createImage();
          String code = imageMap.keySet().iterator().next();
          request.getSession().setAttribute("code", code);       
          BufferedImage image = imageMap.get(code);
          response.setContentType("image/jpeg");
          ops = response.getOutputStream();
		  ImageIO.write(image, "jpeg", ops);
	  } catch (IOException e) {
		e.printStackTrace();
	  }finally{
		  if (ops!=null) {
			 ops.close();  
		}		  
	  }  
  }
}
